#!/usr/bin/env python 
# -*- coding:utf-8 -*-

import pandas as pd
pd.set_option('display.max_columns', None)
#显示所有行
pd.set_option('display.max_rows', None)
def precision(data, topK):
    data = data.groupby('userid').apply(lambda x: x.sort_values(by="rating", ascending=False)).reset_index(drop=True).apply(list)
    data_real = data.groupby('userid')['rating'].apply(list)
    data_pre = data.groupby('userid')['pre_rating'].apply(list)
    data_real = data_real.values.tolist()
    data_pre = data_pre.values.tolist()
    # print(data)

    p = 0
    for i in range(len(data_real)):
        temp_p = 0
        for j in range(len(data_real[i])):
            if j >= topK:
                continue
            # if data_real[i][j] >= 3 and data_pre[i][j] >= 3.0 or abs(data_real[i][j] - data_pre[i][j]) <= 1:
            if data_real[i][j]>=3.0 and data_pre[i][j]>=3.0:
                temp_p += 1
        p += temp_p/topK
    p = p/len(data_real)
    return p

# model = 'VAE'
model = 'LGCN'
# model = 'MLP'
# model = 'GCMC'

data_num = "1k"
# data_num = "5k"
# data_num = "1w"
dataset = 'dvd'
# dataset = 'ml'
# dataset = 'fsl'

# messageSpread = ''
# messageSpread = 'ms_'
messageSpread = 'sms_'
# messageSpread = 'sms_'

# if messageSpread:
#     data = pd.read_csv('../file_saved/ms_{}{}-{}.csv'.format( model,dataset, data_num))
# else:
data = pd.read_csv('../file_saved/{}{}{}-{}.csv'.format(messageSpread,model,dataset,data_num))
# data = pd.read_csv('../file_saved/MLPresult-dvd-1k.csv')
# data = pd.read_csv('../file_saved/MLPresult-movielens-1k.csv')
print('model:{}     data:{}-{}'.format(model,dataset,data_num))
print('precision')
print('top5\t\t\t\t\t\ttop10')
p5 = precision(data,5)
p10 = precision(data,10)
p20 = precision(data,20)
print(p5,'\t',p10)
# print(p5/p10)







